package com.zlp.fdfs.boot.service.impl;


import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.zlp.fdfs.boot.entity.PicUrl;
import com.zlp.fdfs.boot.service.ImageService;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;


/**
 * @author Lei.Zhang
 * @Title IImageService
 * description: TODO
 * @Date 2021/1/20 17:00
 */
@Service
public class IImageService implements ImageService {

    @Autowired
    PicUrl picUrl;

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    private static final Logger logger = LoggerFactory.getLogger(IImageService.class);

    @Override
    public String uploadImage(MultipartFile file) {
        StorePath storePath = null;
        InputStream inputStream = null;
        ByteArrayInputStream byteArrayInputStream = null;
        ByteArrayInputStream thumbImageStream = null;
        //校验图片格式
        String originalFilename = file.getOriginalFilename();
        //获取后缀名
        String type = FilenameUtils.getExtension(originalFilename);


        if (!type.toUpperCase().equals("JPG")) {
            try {
                inputStream = file.getInputStream();
                //先转换为jpg
                byteArrayInputStream = convertPictureFormat(inputStream);
                thumbImageStream = getThumbImageStream(byteArrayInputStream);
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }
        int available = thumbImageStream.available();
        storePath = fastFileStorageClient.uploadFile(thumbImageStream, available, type, null);
        return storePath.getFullPath();
    }

    /**
     * //图片尺寸不变，压缩图片文件大小outputQuality实现,参数1为最高质量
     * Thumbnails.of(fromPic).scale(1f).outputQuality(0.25f).toFile(toPic);
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    private ByteArrayInputStream getThumbImageStream(InputStream inputStream) throws IOException {
        // 在内存当中生成缩略图
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        //@formatter:off
        Thumbnails.of(inputStream).scale(1f).outputQuality(0.25f).toOutputStream(out);
        //@formatter:on
        return new ByteArrayInputStream(out.toByteArray());
    }

    /**
     * @param inputStream 流
     */
    private ByteArrayInputStream convertPictureFormat(InputStream inputStream) throws IOException {
        // 在内存当中生成缩略图
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        //@formatter:off
        Thumbnails.of(inputStream).size(2560, 2048).outputFormat("jpg").toOutputStream(out);
        //@formatter:on
        return new ByteArrayInputStream(out.toByteArray());
    }

    @Override
    public void deletedImage(String url) {
        String imageUrl = getImageUrl(url);
        fastFileStorageClient.deleteFile(imageUrl);
    }

    private String getImageUrl(String url) {
        String substring = url.substring(picUrl.getDomainName().length());
        return substring;
    }
}
